﻿#include "setup5wind.h"
#include "qevent.h"
#include "ui_setup5wind.h"
#include <mainwindow.h>
#include "speechmodulethread.h"
extern intListener doorStatus;
QString comtest;
int speflv;

setup5wind::setup5wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup5wind)
{
    ui->setupUi(this);
    ui->radioButton->setChecked(true);
    mpShadeWindow = new QWidget(this);
    speechmd = new speechModuleThread();
    ui->radioButton_3->hide();

    QIntValidator *validator = new QIntValidator(this);
    validator->setRange(0, 65535); // 设置接受的数字范围
    QRegExp regExp("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    QRegExpValidator *validator2 = new QRegExpValidator(regExp, this);
    ui->ip_le_2->setValidator(validator2);
    ui->port_le_2->setValidator(validator);
    ui->tabWidget->setStyleSheet("QTabWidget:pane {border-top:0px solid #e8f3f9;background:  transparent; }"
                                 "QTabBar::tab { background-color: transparent; }");

    QObject::connect(&MainWindow::tcpStatus, &intListener::valueChanged, this,&setup5wind::tcpStatusChanged);
    connect(&doorStatus, &intListener::valueChanged,this,&setup5wind::doorStatusChanged,Qt::QueuedConnection);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->tabWidget->setFont(font);
            ui->label->setFont(font);
            ui->radioButton->setFont(font);
            ui->radioButton_2->setFont(font);
            ui->radioButton_3->setFont(font);
            ui->label_11->setFont(font);
            ui->label_12->setFont(font);
            ui->label_13->setFont(font);
            ui->label_14->setFont(font);
            ui->ip_le_2->setFont(font);
            ui->port_le_2->setFont(font);
            ui->ck_cb1_2->setFont(font);
            ui->ck_cb2_2->setFont(font);
            ui->label_7->setFont(font);
            ui->comboBox->setFont(font);
            ui->SaveBtn_2->setFont(font);
            ui->SaveBtn->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnBtn->setFont(familyName);
            ui->pushButton_9->setFont(familyName);
        }
    }
#endif
}
void setup5wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
}

void setup5wind::tcpStatusChanged(){
    if(MainWindow::tcpStatus.get()==1){
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
        ui->label_6->setText("未连接");
    }else{
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
        ui->label_6->setText("已连接");
    }
}

setup5wind::~setup5wind()
{
    delete ui;
}

void setup5wind::showEvent(QShowEvent *event)
{
#ifdef Q_OS_ANDROID
    if(MainWindow::experimentStatus.get()==0){
        ui->radioButton_3->show();
    }
    else
    {
        ui->radioButton_3->hide();
    }
#endif
    tcpClient=TcpClient::getInstance();
    ui->ck_cb1_2->clear();
    ui->comboBox->clear();
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {
        ui->ck_cb1_2->addItem(info.portName());
    }
    QString sql="Select * from ConnectionInfo";

    ui->comboBox->blockSignals(true);
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {
        ui->comboBox->addItem(info.portName());
    }
    ui->comboBox->setCurrentText("ttyS4");
    ui->comboBox->blockSignals(false);

    QSqlDatabase db = dbHelper::openConnection();

    QSqlQuery query(db);
    query.exec(sql);
    while (query.next()) {
        ui->ip_le_2->setText(query.value("Ip").toString());
        ui->port_le_2->setText(query.value("Port").toString());
    }

    if(tcpClient->IsOnline()){
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
        ui->label_6->setText("已连接");
    }else{
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
        ui->label_6->setText("未连接");
    }


    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
    refShadeWindow();
    event->accept();
}

void setup5wind::on_SaveBtn_clicked()
{
    if(ui->tabWidget->currentIndex()==0){
        if(MainWindow::connectType==0){
            if(ui->ip_le_2->text()!=nullptr && ui->port_le_2->text()!=nullptr){
                QString ip = ui->ip_le_2->text();
                quint16 port = ui->port_le_2->text().toUInt();

                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString insertSql= QString("update ConnectionInfo set IP='%1',Port='%2';").arg(ip,QString::number(port));
                query.exec(insertSql);

                tcpClient= TcpClient::getInstance();
                tcpClient->ClientConnectToHost(ip, port);

                thread()->sleep(1);

                if(MainWindow::tcpStatus.get()==1)
                {
                    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "                              连接失败                                    ", QMessageBox::Yes, this);
                    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
                    box->exec();

                    ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
                    ui->label_6->setText("未连接");
                }
                else
                {
                    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "                              连接成功                                    ", QMessageBox::Yes, this);
                    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
                    box->exec();
                    ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
                    ui->label_6->setText("已连接");
                }
            }
        }
        else if(MainWindow::connectType==1)
        {
            QString serialPort = ui->ck_cb1_2->currentText();
            QString baudRate = ui->ck_cb2_2->currentText();

            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString insertSql= QString("update ConnectionInfo set SerialPort='%1',BaudRate='%2';").arg(serialPort,baudRate);
            query.exec(insertSql);

            serialClient=SerialportThread::getInstance();
            if(MainWindow::serialStatus.get()==1){
                serialClient->serialclose();
            }
            serialClient->ClientConnectToHost(serialPort,baudRate);

            thread()->sleep(1);
            if(MainWindow::serialStatus.get()==1)
            {
                QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "                              连接成功                                    ", QMessageBox::Yes, this);
                box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
                box->exec();

                ui->label_5->setStyleSheet("border-image: url(:/pic/picture/ck1.png);");
                ui->label_6->setText("已连接");
            }
            else
            {
                QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "                              连接失败                                    ", QMessageBox::Yes, this);
                box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
                box->exec();
                ui->label_5->setStyleSheet("border-image: url(:/pic/picture/ck0.png);");
                ui->label_6->setText("未连接");
            }
        }
    }
    else if(ui->tabWidget->currentIndex()==1){
        speflv = 1;
        comtest = ui->comboBox->currentText();
        MainWindow::speechflagkg.set(0);
    }
}

void setup5wind::resizeEvent(QResizeEvent *event){
    refShadeWindow();
}

void setup5wind::refShadeWindow(){
    if(mpShadeWindow){
        mpShadeWindow->close();
    }

    bool isFirst=false;
    int guideStatus;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select * From IsFirstStartUp";

    query.exec(sql);

    while (query.next()) {
        isFirst = query.value("IsFirst").toString()=="0";
        guideStatus = query.value("GuideStatus").toInt();
    }
    query.clear();

    if(isFirst){
        if(guideStatus==0)//网络连接
        {
            mpShadeWindow = new QWidget(this);
            QString str("QWidget{background-color:rgba(0,0,0,0.6);}");   //0.6代表透明度，根据需要自行调整
            mpShadeWindow->setStyleSheet(str);
            QRect rect = this->geometry();
            int screenWidth = rect.width();  // 屏幕宽度
            int screenHeight = rect.height();  // 屏幕高度
            mpShadeWindow->setGeometry(0, 0,screenWidth,screenHeight);    //遮罩窗口位置

            QRect btnRect= ui->SaveBtn->frameGeometry();

            QLabel *label=new QLabel("1/4网络连接",mpShadeWindow);
            QPalette palette;
            palette.setColor(QPalette::WindowText, QColor(255,255,255));
            QFont font=label->font();
            font.setPointSize(24);
            label->setFont(font);
            label->setPalette(palette);
            label->setGeometry(btnRect.x()-40,screenHeight-160,180,30);
            label->setStyleSheet("background-color:rgba(0,0,0,0);");

            QPushButton * btnSkipThis = new QPushButton("跳过当前引导",mpShadeWindow);
            btnSkipThis->setGeometry(btnRect.x()-110,screenHeight-120,120,40);
            btnSkipThis->setStyleSheet("border-radius: 8px; background-color:#FFFFFF");

            QObject::connect(btnSkipThis, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set GuideStatus=1";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton * btnSkipAll = new QPushButton("跳过全部引导",mpShadeWindow);
            btnSkipAll->setGeometry(btnRect.x()+20,screenHeight-120,120,40);
            btnSkipAll->setStyleSheet("border-radius: 8px; background-color:#2F42CD");

            QObject::connect(btnSkipAll, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set IsFirst='1',GuideStatus=5";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton *btnSet=new QPushButton("连接",mpShadeWindow);
            btnSet->setGeometry(btnRect.x(),screenHeight-60,120,40);
            QFont btnFont=btnSet->font();
            font.setPointSize(12);
            btnSet->setFont(font);
            btnSet->setStyleSheet("background-color: qlineargradient(x1: 0, y0: 0, x2: 0, y2: 1, stop: 0 #8C97ED, stop: 1 #0018D1);border-radius: 8px; color: white;");

            QLabel *movieLabel=new QLabel("",mpShadeWindow);
            movieLabel->setGeometry(btnRect.x(),screenHeight-40,60,60);
            movieLabel->setStyleSheet("background-color:rgba(0,0,0,0);");

            QMovie  *guideMovie = new QMovie(":/pic/picture/guide.gif");
            movieLabel->setMovie(guideMovie);
            guideMovie->start();

            QObject::connect(btnSet, &QPushButton::clicked,[this](){
                mpShadeWindow->close();

                mpShadeWindow = new QWidget(this);
                QString str("QWidget{background-color:rgba(0,0,0,0.6);}");   //0.6代表透明度，根据需要自行调整
                mpShadeWindow->setStyleSheet(str);
                QRect rect = this->geometry();
                int screenWidth = rect.width();  // 屏幕宽度
                int screenHeight = rect.height();  // 屏幕高度
                mpShadeWindow->setGeometry(0, 0,screenWidth,screenHeight);    //遮罩窗口位置

                QRect btnRect= ui->returnBtn->frameGeometry();

                QLabel *label=new QLabel("1/4网络连接",mpShadeWindow);
                QPalette palette;
                palette.setColor(QPalette::WindowText, QColor(255,255,255));
                QFont font=label->font();
                font.setPointSize(24);
                label->setFont(font);
                label->setPalette(palette);
                label->setGeometry(btnRect.x()+40,75,180,30);
                label->setStyleSheet("background-color:rgba(0,0,0,0);");

                QPushButton * btnSkipThis = new QPushButton("跳过当前引导",mpShadeWindow);
                btnSkipThis->setGeometry(btnRect.x(),120,120,40);
                btnSkipThis->setStyleSheet("border-radius: 8px; background-color:#FFFFFF");

                QObject::connect(btnSkipThis, &QPushButton::clicked,[this](){
                    QSqlDatabase db = dbHelper::openConnection();
                    QSqlQuery query(db);
                    QString sql="Update IsFirstStartUp set GuideStatus=1";
                    query.exec(sql);

                    refShadeWindow();
                });

                QPushButton * btnSkipAll = new QPushButton("跳过全部引导",mpShadeWindow);
                btnSkipAll->setGeometry(btnRect.x()+120,120,120,40);
                btnSkipAll->setStyleSheet("border-radius: 8px; background-color:#2F42CD");

                QObject::connect(btnSkipAll, &QPushButton::clicked,[this](){
                    QSqlDatabase db = dbHelper::openConnection();
                    QSqlQuery query(db);
                    QString sql="Update IsFirstStartUp set IsFirst='1',GuideStatus=5";
                    query.exec(sql);
                    query.clear();

                    refShadeWindow();
                });

                QPushButton *btnSet=new QPushButton("返回",mpShadeWindow);
                btnSet->setGeometry(btnRect.x(),0,92,56);
                btnSet->setStyleSheet("border-image: url(:/pic/picture/t9.png);background-color: rgba(0, 0, 0, 0.3);");

                QLabel *movieLabel=new QLabel("",mpShadeWindow);
                movieLabel->setGeometry(btnRect.x(),40,60,60);
                movieLabel->setStyleSheet("background-color:rgba(0,0,0,0);");

                QMovie  *guideMovie = new QMovie(":/pic/picture/guide.gif");
                movieLabel->setMovie(guideMovie);
                guideMovie->start();

                QObject::connect(btnSet, &QPushButton::clicked,[this](){
                    QSqlDatabase db = dbHelper::openConnection();
                    QSqlQuery query(db);

                    QString sql="Update IsFirstStartUp set GuideStatus=1";
                    query.exec(sql);

                    this->close();
                    emit changeTotalWindow();
                });
                mpShadeWindow->show();

            });

            mpShadeWindow->show();
        }
    }
}

void setup5wind::on_SaveBtn_2_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup5wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup5wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        关闭仓门");
        doorStatus.set(1);
    }
    else
    {
        ui->pushButton_9->setText("        打开仓门");
        doorStatus.set(0);
    }
}


void setup5wind::on_comboBox_currentTextChanged(const QString &arg1)
{

}

void setup5wind::on_radioButton_clicked(bool checked)
{
    if(checked){
        ui->stackedWidget->setCurrentIndex(0);
        MainWindow::connectType=0;
    }
}

void setup5wind::on_radioButton_2_clicked(bool checked)
{
    if(checked){
        ui->stackedWidget->setCurrentIndex(1);
        MainWindow::connectType=1;
    }
}


void setup5wind::on_radioButton_3_clicked(bool checked)
{
    if(checked){
        ui->stackedWidget->setCurrentIndex(2);
        MainWindow::connectType=2;
    }
}
